<template>
  <div class="layout-container antiJam-test-sample-selected">
    <FlowStep :activeStep="2" />
    <div class="visual-box">
      <BaseInfoForm :showTitle="true" :sampleId="route.query.sampleId" />
      <TagIcon title="融合计算结果" :style="tagIconStyle" />
      <div>
        <CommonTable
          :data="multiFuseTable"
          highlight-current-row
          v-model:page="page"
          v-loading="loading"
          :showPagination="false"
          :show-selection="false"
          style="height: '0'; margin-top: 10px"
          @current-change="handleCurrentChange"
          @onQuery="onQuery"
        >
          <el-table-column prop="hisSampleName" label="历史试验数据名称" />
          <el-table-column prop="value" label="匹配度" />
          <el-table-column prop="name" label="相似结果" />
        </CommonTable>
      </div>

      <TagIcon title="静态特征相似性结果" :style="tagIconStyle" />
      <div>
        <CommonTable
          :data="staticTable"
          v-model:page="page"
          v-loading="loading"
          :showPagination="false"
          :show-selection="false"
          style="height: '0'; margin-top: 10px"
          @onQuery="onQuery"
        >
        <el-table-column prop="hisSampleName" label="历史试验数据名称" />
          <el-table-column label="关键特征匹配度" align="center">
            <template v-for="(value,key) in cols" :key="key">         
              <el-table-column :prop="key" :label="key" />     
            </template>
          </el-table-column>
        </CommonTable>
      </div>

      <TagIcon title="动态特征相似性结果" :style="tagIconStyle" />
      <div>
        <CommonTable
          :data="dynmicTable"
          v-model:page="page"
          v-loading="loading"
          :showPagination="false"
          :show-selection="false"
          style="height: '0'; margin-top: 10px"
          @onQuery="onQuery"
        >
          <el-table-column
            prop="hisSampleName"
            label="历史试验数据名称"
            width="140"
          />
          <el-table-column label="基于动态时间弯曲的序列距离" align="center">
            <el-table-column
              prop="baseInfoDegree"
              label="试验样本基础信息匹配度"
            />
            <el-table-column
              prop="staticParaDegree"
              label="试验场景说明描述匹配度"
            />
            <el-table-column
              prop="dynamicParaDegree"
              label="战技使用描述匹配度"
            />
            <el-table-column prop="matchDegree" label="整体加权匹配度" />
          </el-table-column>
        </CommonTable>

        <CommonTable
          :data="dynmicTable2"
          v-model:page="page"
          v-loading="loading"
          :showPagination="false"
          :show-selection="false"
          style="height: '0'; margin-top: 10px"
          @onQuery="onQuery"
        >
          <el-table-column
            prop="hisSampleName"
            label="历史试验数据名称"
            width="140"
          />
          <el-table-column label="基于灰色关联度的形状相似性" align="center">
            <el-table-column
              prop="baseInfoDegree"
              label="试验样本基础信息匹配度"
            />
            <el-table-column
              prop="staticParaDegree"
              label="试验场景说明描述匹配度"
            />
            <el-table-column
              prop="dynamicParaDegree"
              label="战技使用描述匹配度"
            />
            <el-table-column prop="matchDegree" label="整体加权匹配度" />
          </el-table-column>
        </CommonTable>

        <CommonTable
          :data="dynmicTable3"
          v-model:page="page"
          v-loading="loading"
          :showPagination="false"
          :show-selection="false"
          style="height: '0'; margin-top: 10px"
          @onQuery="onQuery"
        >
          <el-table-column
            prop="hisSampleName"
            label="历史试验数据名称"
            width="140"
          />
          <el-table-column label="融合注意力机制的动态特征向量相似度" align="center">
            <el-table-column
              prop="baseInfoDegree"
              label="试验样本基础信息匹配度"
            />
            <el-table-column
              prop="staticParaDegree"
              label="试验场景说明描述匹配度"
            />
            <el-table-column
              prop="dynamicParaDegree"
              label="战技使用描述匹配度"
            />
            <el-table-column prop="matchDegree" label="整体加权匹配度" />
          </el-table-column>
        </CommonTable>
      </div>

      

    </div>
    <div class="step-buttons common-buttons">
      <el-button type="primary" @click="onBack">上一步</el-button>
      <el-button type="primary" @click="onStartCalculate">开始计算</el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue';
import { Search } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';
import CommonTable from '@/components/common-table/index.vue';
import { Page } from '@/components/common-table/type';
import TagIcon from '@/components/tag-icon/index.vue';
import BaseInfoForm from '../../BaseInfoForm/index.vue';
import {
  httpResultVisiualDisplayList,
  httpResultVisiualDisplayExportFile,
} from '@/api/similarityEvaluationSystem';
import FlowStep from '../../FlowStep/index.vue';
import { httpDeviationFuseComputing } from '@/api/antiJamPerformCalculateSystem';

let $router = useRouter();
const route = useRoute();

const tagIconStyle = reactive({ paddingLeft: '16px', paddingBottom: 0 });

// 静态表格
const staticTable = ref<any>([]);
const currentRow = ref();

// 动态表格
const dynmicTable = ref<any>([]);
const dynmicTable2 = ref<any>([]);
const dynmicTable3 = ref<any>([]);
// 多种融合
const multiFuseTable = ref<any>([]);

const loading = ref<boolean>(false);
const page = reactive<Page>({
  current: 1,
  pageSize: 20,
});
const cols = ref<any>([]);

onMounted(() => {
  onQuery({ pageNo: 1 });
});

// 查询数据
const onQuery = (val: any) => {
  loading.value = true;
  httpResultVisiualDisplayList({ sampleId: route.query.sampleId }).then(
    (res) => {
      const result = res.data;
      cols.value = JSON.parse(result.staticResult[0].keyParaResult);
      loading.value = false;
      const { staticResult, dynamic, fusion } = result;

      // 静态相似性
      const staticData = staticResult.map(item => {
        const keyParaResult = JSON.parse(item.keyParaResult)
        return { ...item, ...keyParaResult }
      })
      staticTable.value = staticData;

      // 动态相似性
      dynmicTable.value = dynamic;
      dynmicTable.value.map((item: any) => {
        if (item.computingMode === 'DTW') {
          item.computingModeName = '基于动态时间弯曲的序列距离';
        } else if (item.computingMode === 'shape') {
          item.computingModeName = '基于灰色关联度的形状相似性';
        } else {
          item.computingModeName = '融合注意力机制的动态特征向量相似度';
        }
      });
      dynmicTable.value = filterDWTData(dynamic);
      dynmicTable2.value = filterShapData(dynamic);
      dynmicTable3.value = filterCosineData(dynamic);

      // 多种融合
      multiFuseTable.value = fusion;
    }
  );
};

// 过滤出DTW数据
const filterDWTData = (data: any) => {
  if (data && data.length) {
    return data.filter((item: any) => item.computingMode === 'DTW');
  } else {
    return [];
  }
};

// 过滤出Shap数据
const filterShapData = (data: any) => {
  if (data && data.length) {
    return data.filter((item: any) => item.computingMode === 'shape');
  } else {
    return [];
  }
};

// 过滤出cosine数据
const filterCosineData = (data: any) => {
  if (data && data.length) {
    return data.filter((item: any) => item.computingMode === 'cosine');
  } else {
    return [];
  }
};

// 过滤出keys
const filterKeys = (arr: []) => {
  let keys: string[] = [];
  for (let item of arr) {
    keys.push(item.id);
  }
  return keys;
};

// 上一步
const onBack = () => {
  $router.push({
    path: '/antiJamPerformCalculateSystem/testSampleSelected',
    query: {},
  });
};

// 选择监听器
const handleCurrentChange = (val: any) => {
  currentRow.value = val;
  console.log('currentRow=', currentRow.value)
};

// 开始计算
const onStartCalculate = () => {
  if (!currentRow.value) {
    ElMessage({
      message: '请选择一条历史试验数据',
      type: 'warning',
    });
    return;
  }

  if (currentRow.value.name == '不相似' || currentRow.value.name == '一致') {
    ElMessage({
      message: '当前选择的历史试验数据与当前试验数据不相似，请选择相似的!',
      type: 'warning',
    });
    return;
  }

  httpDeviationFuseComputing({
    sampleId: route.query.sampleId,
    hisSampleId: currentRow.value.hisSampleId,
  }).then((res) => {
    const result = res.data;
    const fuseResult = result['deviationFuseResult'];
    console.log('hisSampleId=', currentRow.value.hisSampleId)
    $router.push({
      path: '/antiJamPerformCalculateSystem/computeAtlas',
      query: {
        hisSampleId: currentRow.value.hisSampleId,
        sampleId: route.query.sampleId,
        sampleName: route.query.sampleName,
        deviationFuseResult: fuseResult
      },
    });
 });
};
</script>

<style lang="scss" scoped>
@import '@/assets/style/buttons';
.antiJam-test-sample-selected {
  .layout-container-table {
    padding: 3px 15px;
    .el-button--small {
      background: transparent;
      color: rgba(24, 81, 237, 1);
    }
  }
  .visual-box {
    height: calc(100vh - 120px);
    overflow: auto;
    overflow-y: scroll;
    overflow-x: hidden;
  }
  .step-buttons {
    display: flex;
    justify-content: center;
    box-shadow: 0px 0px 10px rgba(218, 222, 224, 1);
    padding: 10px 0;
  }
}
.el-button {
  background: rgb(1, 18, 90);
  border: 0;
}
</style>
